//
//  CompanyBodyView.swift
//  AirlineReservation
//
//  Created by Kevin on 2020/2/16.
//  Copyright © 2020 Kevin. All rights reserved.
//

import UIKit

public protocol CompanyBodyViewDelegate {
    // 保存
    func company_save(item:CallbackBean,isShowMore:Bool)
    // 删除
    func company_delete(company:CompanyExpenseInfo,isShowMore:Bool)
}


class CompanyBodyView: UIView {
    
    // 常量
    private let titles = ["公司地址","公司电话","银行名称","银行账号"]
    private let placeholders = ["必填","必填","必填，如‘中国银行’","必填"]
    // 存储
    private var holderViews = [TextFieldOffsetPlaceHolder]()
    // 是否显示删除按钮
    private let deleteLabel = UILabel(title: "删除该报销", color: UIColor.orange, size: 14, align: .center)
    // 头部
    private let top = CompanyTopView()
    // 滑动
    private var scrollview = UIScrollView()
    // 代理
    public var delegate:CompanyBodyViewDelegate?
    
    // 是否显示专业发票
    private var isShowMore:Bool = false {
        didSet {
            updateLayout()
        }
    }
    
    // 赋值
    public var companyExpenseInfo:CompanyExpenseInfo? {
        didSet {
            top.holderViews[0].text = companyExpenseInfo?.title
            top.holderViews[1].text = companyExpenseInfo?.duty
            if let address = companyExpenseInfo?.companyaddress {
                isShowMore = true
                holderViews[0].text = address
                holderViews[1].text = companyExpenseInfo?.companymobile
                holderViews[2].text = companyExpenseInfo?.bankname
                holderViews[3].text = companyExpenseInfo?.bankaccount
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        init_views()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        // 头部
        top.backgroundColor = UIColor.white
        top.frame = CGRect(x: 0, y: 0, width: frame.width, height: 133)
        scrollview.addSubview(top)
        
        // 背景色
        let back = UIView()
        back.backgroundColor = UIColor.white
        scrollview.addSubview(back)
        // 宽度
        let w = frame.width - MARGIN * 2
        // 文本
        let labelY = top.frame.maxY + MARGIN10
        let label = UILabel(title: "需要增值税专用发票", color: COLOR_NICE, size: 15)
        label.frame = CGRect(x: MARGIN, y: labelY, width: w, height: MARGIN60)
        scrollview.addSubview(label)
        // 开关
        let uiSwitch = UISwitch()
        uiSwitch.isOn = isShowMore
        let uiSize = uiSwitch.bounds.size
        uiSwitch.onTintColor = UIColor.orange
        let uiSwitchY = (MARGIN60 - uiSize.height) * 0.5 + labelY
        uiSwitch.frame.origin = CGPoint(x: frame.width - MARGIN - uiSize.width, y: uiSwitchY)
        scrollview.addSubview(uiSwitch)
        // 添加点击事件
        uiSwitch.addTarget(self, action: #selector(clickSwichItem(ui:)), for: .valueChanged)
        // 记录高度
        var tempHeight:CGFloat = MARGIN60
        // 专用发票
        if isShowMore {
            (0..<titles.count).forEach { (i) in
                let labelY = label.frame.maxY + CGFloat(i) * MARGIN70 + MARGIN
                // 标题
                let label = UILabel(title: titles[i], color: COLOR_NICE, size: 15)
                label.frame = CGRect(x: MARGIN, y: labelY, width: MARGIN * 4.8, height: MARGIN30)
                scrollview.addSubview(label)
                // 输入
                let input = holderViews[i]
                input.frame = CGRect(x: MARGIN, y: labelY, width: w, height: MARGIN30)
                scrollview.addSubview(input)
                // 下划线
                let line = UIView()
                line.backgroundColor = COLOR_LINE
                let lineY = input.frame.maxY + MARGIN
                line.frame = CGRect(x: MARGIN, y: lineY, width: w, height: 1)
                if i != titles.count - 1 {
                    scrollview.addSubview(line)
                }
                // 处理最后一个下划线
                tempHeight = line.frame.maxY
            }
            tempHeight -= top.frame.maxY
        }
        // 背景 frame
        back.frame = CGRect(x: 0, y: top.frame.maxY + MARGIN10, width: frame.width, height: tempHeight)
        // 确认修改按钮
        let saveBtn = UIButton(title: "保存", name: "button_orange",radius: 5)
        saveBtn.frame = CGRect(x: MARGIN, y: back.frame.maxY + MARGIN25, width: w, height: MARGIN50)
        scrollview.addSubview(saveBtn)
        // 确认修改按钮 添加点击事件
        saveBtn.addTarget(self, action: #selector(clickItem), for: .touchUpInside)
        addSubview(scrollview)
        // 是否显示删除按钮
        deleteLabel.frame = CGRect(x: 0, y: saveBtn.frame.maxY + MARGIN20, width: frame.width, height: MARGIN)
        if companyExpenseInfo != nil  {
            scrollview.addSubview(deleteLabel)
            // 添加点击事件
            deleteLabel.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(clickDeleteItem)))
        }
        // 设置scrollview属性
        scrollview.delegate = self
        scrollview.backgroundColor = COLOR_LIGHT_WHITE
        scrollview.showsVerticalScrollIndicator = false
        scrollview.frame = CGRect(x: 0, y: 0, width: frame.width, height: frame.height)
        scrollview.contentSize = CGSize(width: frame.width, height: deleteLabel.frame.maxY + frame.height * 0.25)
    }
    
    @objc func clickSwichItem(ui:UISwitch) -> Void {
        isShowMore = ui.isOn
    }
    
    @objc func clickItem() -> Void {
        var bean = CallbackBean(firstIn: getInput(i: 0), secondIn: getInput(i: 1))
        if isShowMore {
            bean = CallbackBean(firstIn: getInput(i: 0), secondIn: getInput(i: 1),threeIn: getInput(i: 2),fourIn: getInput(i: 3),fiveIn: getInput(i: 4),sexIn: getInput(i: 5))
        }
        delegate?.company_save(item: bean,isShowMore: isShowMore)
    }
    
    @objc func clickDeleteItem() -> Void {
        guard let company = companyExpenseInfo else {
            return
        }
        delegate?.company_delete(company: company, isShowMore: isShowMore)
    }
    
    func getInput(i:Int) -> String? {
        if i > 1 {
            return holderViews[i - 2].text
        }
        return top.holderViews[i].text
    }
}

extension CompanyBodyView:UIScrollViewDelegate {
    
    private func init_views() -> Void {
        (0..<titles.count).forEach { (i) in
            let input = TextFieldOffsetPlaceHolder()
            input.placeholder = placeholders[i]
            input.textColor = UIColor.lightGray
            input.font = UIFont.systemFont(ofSize: 13)
            input.setValue(MARGIN * 5, forKey: "paddingLeft")
            holderViews.append(input)
            scrollview.addSubview(input)
        }
    }
    
    func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
        scrollView.endEditing(true)
    }
}

